use askama::Template;
use askama_axum::IntoResponse;
use axum::{extract::State, response::Html};
use tower_sessions::Session;

use crate::{controllers::backend::AdminSession, core::AppState, services::backend::{advert_service::AdvertService, blog_service::BlogService, collect_service::CollectService, product_service::ProductService}, types::common::Breadcrumb};



#[derive(Template)]
#[template(path = "backend/dashboard.html")]
struct DashboardTemplate {
    auth: AdminSession,
    breadcrumb: Breadcrumb,
    blogs_count: i64, 
    products_count: i64, 
    collects_count: i64, 
    adverts_count: i64
}


/// 统计页面
pub async fn dashboard_handler(State(state): State<AppState>, session: Session) -> impl IntoResponse {
    let auth = AdminSession::init(session).await;
    let breadcrumb = Breadcrumb {
        title: "管理后台".to_string(),
        sub_title: "首页".to_string(),
    };

    // 文章数量
    let blogs_count = BlogService::all_count(&state.db).await;
    // 产品数量
    let products_count = ProductService::all_count(&state.db).await;
    // 收藏数量
    let collects_count = CollectService::all_count(&state.db).await;
    // 广告数量
    let adverts_count = AdvertService::all_count(&state.db).await;
    let template = DashboardTemplate {
        auth,
        breadcrumb,
        blogs_count,
        products_count,
        collects_count,
        adverts_count
    };

    Html(template.render().unwrap())
}
